package com.waylau.spring.cloud.weather.service;

import com.waylau.spring.cloud.weather.vo.City;
import com.waylau.spring.cloud.weather.vo.CityList;
import com.waylau.spring.util.XmlBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

@Service
public class CityDataServiceImpl implements CityDataService {

    @Override
    public List<City> cityList() throws  Exception {



        Resource resource = new ClassPathResource("citylist.xml");

        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"utf-8"));

        StringBuffer stringBuffer = new StringBuffer();

        String lin = "";

        while ((lin=br.readLine()) != null){
            stringBuffer.append(lin);
        }

        br.close();

        CityList  cityList= (CityList) XmlBuilder.xmlStrToOject(CityList.class,stringBuffer.toString());

        return cityList.getCityList();
    }
}
